<html>
<head>
<script >
Date.prototype.setISO8601 = function (string) {
var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
	"(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
	"(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
var d = string.match(new RegExp(regexp));

var offset = 0;
var date = new Date(d[1], 0, 1);

if (d[3]) { date.setMonth(d[3] - 1); }
if (d[5]) { date.setDate(d[5]); }
if (d[7]) { date.setHours(d[7]); }
if (d[8]) { date.setMinutes(d[8]); }
if (d[10]) { date.setSeconds(d[10]); }
if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
if (d[14]) {
	offset = (Number(d[16]) * 60) + Number(d[17]);
	offset *= ((d[15] == '-') ? 1 : -1);
}

offset -= date.getTimezoneOffset();
time = (Number(date) + (offset * 60 * 1000));
this.setTime(Number(time));
}


var timer;
var twiter_friend_count=0;
var twitter_firend_array=new Array();
var bool_get_chart=true;
var total_count=0;
var func_end=false;
var chart_color=new Array("802B00","806A00","2B0080","6A0080","558000","7C00DB","336699","88C700","999933","CC9900");
function twitterfirend()
{
    this.name="";
    this.speed=0.0;
}
function getHealth(index)
{
    var cur=parseInt(twitter_firend_array[index].speed*10)
    if(cur==0)
    {
        return 0;
    }
    else
    {
        return 10*Math.log(cur);
    }
}
function getLabel(i)
{
    return twitter_firend_array[i].name+"+"+parseInt(getHealth(i));
}
function getChartURL(StartIndex)
{
    chart_color.sort(function() {return 0.5 - Math.random()});
    var end=StartIndex+10;
    var auto_size=true;
    if(twiter_friend_count<StartIndex+10)
    {
        end=twiter_friend_count;
        
    }
    //http://chart.apis.google.com/chart?cht=bvg&chs=1000x200&chd=t:20|30|40&chdl=aaa|bbb|ccc&chco=fff0f0,ff00f0,ff0000
    var chart_url="http://chart.apis.google.com/chart?";
    chart_url+="cht=bvg&";
    chart_url+=("chs=500x200&");
    chart_url+="chd=t:";
    
    //CHART DATA
    chart_url+=getHealth(StartIndex);
    for(var i=StartIndex+1;i<end;i++)
    {
        chart_url+="|";
        chart_url+=getHealth(i);
    }
    
    //CHART LABEL
    chart_url+="&chdl=";
    chart_url+=getLabel(0,StartIndex);
    for(var i=StartIndex+1;i<end;i++)
    {
        chart_url+="|";
        chart_url+=getLabel(i,StartIndex);

    }
        
    chart_url+="&chco=";
    chart_url+=chart_color[StartIndex%chart_color.length];
    for(var i=StartIndex+1;i<end;i++)
    {
        chart_url+=",";
        chart_url+=chart_color[i%chart_color.length];
    }
    if(total_count>1)
    {
        chart_url+="&chbh=a";
    }
    else
    {
        chart_url+="&chbh=150";
        chart_url+="&chtt="+twitter_firend_array[StartIndex].name+"'s|TweetActive|Index+is|"+parseInt(getHealth(StartIndex));
    }
    return chart_url;
}
function getchart()
{
       if((twiter_friend_count>=total_count || func_end) && bool_get_chart && total_count!=0)
       {
           var chart_url="";
           twitter_firend_array.sort(function(a,b){return b.speed - a.speed});
            var div=document.getElementById("SPEED");
            div.innerHTML="";
           for(var i=0;i<twiter_friend_count;i+=10)
           {
               chart_url=getChartURL(i);
              
               var chartimage=document.createElement("img");
               chartimage.setAttribute("id","CHART");
               chartimage.setAttribute("src",chart_url);
               var ln_br1=document.createElement("br");
               
               var ln_br2=document.createElement("br");
               div.appendChild(chartimage);
               div.appendChild(ln_br1);
               
               if(twiter_friend_count==1)
               {
                    var a_link=document.createElement("a");
                    var href_url="http://twitter.com/home?status=";
                    href_url+="My+Current+Activity+Index+is+"+parseInt(getHealth(0));
                    href_url+="+http%3a%2f%2ftweetactive.com";
                    a_link.setAttribute("href",href_url);
                    a_link.innerHTML="Tweet This";
                    div.appendChild(a_link);
               }
               div.appendChild(ln_br2);
           }
           bool_get_chart=false;
           clearInterval(timer);
           var wait_div=document.getElementById("WAIT");
           wait_div.innerHTML="";
           
       }
       else if(twiter_friend_count>0)
       {
            func_end=true;
       }

}
function gettimediff(isotime)
{
    var d1=new Date();
    var d2 = new Date(isotime);
    //d2.setISO8601(isotime);
    var convert=24*60*60*1000;
    var d=(d1-d2)/convert;
    return d;
}
function getDataLength(res)
{
	return res.results.length;
}
function getUserName(res)
{
	return res.query;
}
function getCreatedTime(res,i)
{
	return res.results[i].created_at
}
function cbfunc(res)
{
   if(getDataLength(res)>0)
   {
        //var index=res.query.count-1;
        var d1=new Date();
        var d2 = new Date();
        var speed_max=0;
        if(getDataLength(res)>1)
        {
            //d2.setISO8601(res.query.results.span[index].title);
            for(var i=0;i<getDataLength(res);i++)
            {
                var cur_speed=(i+1)/gettimediff(getCreatedTime(res,i));
                if(speed_max<cur_speed)
                {
                    speed_max=cur_speed;
                }
            }
        }
        else if(getDataLength(res)==1)
        {
            speed_max=1/gettimediff(res.query.results.span.title);
            //d2.setISO8601(res.query.results.span.title);
        }
        var div=document.getElementById("SPEED");
        var user_img=document.createElement("img");
        
        
        var username=getUserName(res);
        twitter_firend_array[twiter_friend_count]=new twitterfirend();
        twitter_firend_array[twiter_friend_count].name=username;
        twitter_firend_array[twiter_friend_count].speed=speed_max;
        twiter_friend_count++;
        //user_img.src= "http://chart.apis.google.com/chart?cht=gom&chs=500x100&chd=t:"+((res.query.count/d)*10)+"&chl="+username;
        //div.appendChild(user_img);
        //var lbreak=document.createElement("br");
        //div.appendChild(lbreak);
        func_end=false;
   }
}
function seed(userid)
{
      var div=document.getElementById("SCRIPT");
      var scrpt=document.createElement("script");
      //select * from html where url="http://twitter.com/markandey" and xpath='//span[@class="published"]'
      //scrpt.src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ftwitter.com%2F"+userid+"%22%20and%20xpath%3D'%2F%2Fspan%5B%40class%3D%22published%22%5D'&format=json&callback=cbfunc";
	  scrpt.src="http://search.twitter.com/search.json?callback=cbfunc&q="+userid;
      div.appendChild(scrpt);          
}
function getfriends(res)
{
   if(res!=null)
   {
        total_count=res.length;
        for(var i=0;i<res.length;i++)
        {
            twitter_firend_array[i]=new twitterfirend();
            seed(res[i].screen_name);
        }
        timer=setInterval(getchart,2000);
   }
   else
   {
        alert("limit exceeded ");
   }
}
function init_variables()
{
    bool_get_chart=true;
    total_count=0;
    func_end=false;
    clearInterval(timer);
    twiter_friend_count=0;
    twitter_firend_array=new Array();
    bool_get_chart=true;
    var wait_div=document.getElementById("WAIT");
    wait_div.innerHTML="<"+"span style=\"font-size: 16pt; color: #ff0000\"><"+"strong"+">"+"Loading... </"+"strong></"+"span>";
}
function seedonlyme()
{

      init_variables();
      var twitteridbox=document.getElementById("TWITTERID");
	  var products = twitteridbox.value
	  var product_list=products.split(',');
	  total_count=product_list.length;
	  twitter_firend_array[0]=new twitterfirend()
	  for(var i=0;i<product_list.length;i++)
	  {
		seed(product_list[i]);
	  }
      timer=setInterval(getchart,2000);
      return false;
}
</script>
</head>
<body>
<div style="text-align:center">
<table>
<tr>
<td style="height: 65px">
<form onsubmit="return seedonlyme();" action="" method="post"> 
	<label for="user" >Enter Keywords</label> 
	<input type="text" id="TWITTERID"/> <br />
	enter keywords to compare, seperated by comma e.g (iPhone, omnia)<br />
	
	<input type="button" value="GetList" onclick="seedonlyme();"/> 
</form>
<div id="WAIT"> 
</div> 
 <div id="SPEED"> 
</div> 
<div id="SCRIPT"> 
</div> 
</td>
</tr>
</table>
</div>
</body>
</html>